package com.example.demo.java8;

import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.junit.Test;
import org.msgpack.util.json.JSON;
import org.springframework.web.util.HtmlUtils;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author f_bao
 * @create 2018/5/18
 */
public class StreamDemo {

    public static final String specialRegex = "\\$";
    private static final Pattern regex23 = Pattern.compile(specialRegex);
    private static final Pattern specialStrRegex = Pattern.compile("RDS_CHAR_DOLLAR");

    public static void main(String[] args) {
        System.out.println("共有" + compute(20) + "种走法");
    }
    static int compute(int stair){
        if ( stair <= 0){
            return 0;
        }
        if (stair == 1){
            return 1;
        }
        if (stair == 2){
            return 2;
        }
        return compute(stair-1) + compute(stair-2);
    }




        private static final Pattern titleRegex = Pattern.compile("title=\\\"?(?<title>.*?)\\\"?\\s", Pattern.CASE_INSENSITIVE);


    public static String getStringByCharCount(String text, int zhLen) {
        if (StringUtils.isEmpty(text)) {
            return text;
        }

        int enLen = zhLen * 2;
        // 单字节字符长度
        int byteLen = text.getBytes().length;
        // 把字符平等对待时的字符串长度
        int charLen = text.length();
        // 记录读取进度
        int byteCount = 0;
        // 记录截取位置
        int pos = 0;
        if (byteLen > enLen) {
            for (int i = 0; i < charLen; i++) {
                // 按中文字符计算加2
                if (text.charAt(i) > 255) {
                    byteCount += 2;
                }
                // 按英文字符计算加1
                else {
                    byteCount += 1;
                }
                // 超出时只记下上一个有效位置
                if (byteCount > enLen) {
                    pos = i;
                    break;
                }
                // 记下当前位置
                if (byteCount == enLen) {
                    pos = i + 1;
                    break;
                }
            }

            if (pos >= 0) {
                return text.substring(0, pos);
            }
        }

        return text;
    }

    public static final String P_NAMED = "(?<year>\\d{4})-(?<md>(?<month>\\d{2})-(?<date>\\d{2}))";
    public static final String DATE_STRING = "2017-04-25";

    private static final Pattern regex = Pattern.compile("</?(strong|span|ol|ul|li|hr|p|a|table|tr|th|td|iframe|img|br|b)[^>]*>");
    private static final Pattern regex22 = Pattern.compile("</?(?<g1>.*?)(\\s|>)");
    @Test
    public void testMatch() {
        String content = "<p>东方明珠</p><p><br></p><p>上海 </p><p><br></p><p>北京</p><p><br></p><p>啊啊啊</p><p><br></p><p>http://you.ctrip.uat.qa.nt.ctripcorp.com/travels/qinghaihu281/3706337.html 连接测试</p><p><img data-id=\"275841040\" phsid=\"275841040\" src=\"https://dimg.uat.qa.nt.ctripcorp.com/images/100j0v00000003kr3F7AB_R_600_10000.jpg\" /></p><p><br></p>";

        String replace = RegexUtils.replace(content, regex, matcher -> {
            String value = matcher.group();

            if (value.contains("/") && StringUtils.equalsIgnoreCase(value, "http")) {
                return value.replace("<", "@GSTravelLT").replace(">", "@GSTravelGT");
            }
            Matcher matcher1 = regex22.matcher(value);
            matcher1.find();
            String tagName = matcher1.group("g1");
            return convertValue(tagName, value, false);
        });
        System.out.println(content);
        System.out.println(replace);

    }

    private static String convertValue(String tagName, String value, boolean isMobile) {
        //img 和 a 不做处理
        if ("a".equalsIgnoreCase(tagName)) {
            return value.replace("<", "@GSTravelLT").replace(">", "@GSTravelGT");
        } else if ("iframe".equalsIgnoreCase(tagName)) {
            String str = value.replace("<", "@GSTravelLT").replace(">", "@GSTravelGT");
            if (isMobile) {
                str = str.replace("height=\"500\"", "height=\"300\"");
            }
            str = str.replace("width=\"600\"", "style=\"width:100%\"");
            return str;
        } else if ("img".equalsIgnoreCase(tagName)) {
            String replace = value.replace("<", "@GSTravelLT").replace(">", "@GSTravelGT");
            return replace;
        } else {
            return "@GSTravelLT" + tagName + "@GSTravelGT";
        }
    }


    @Test
    public void testString() {

        String ragex = " ";
        //切割
        String strs = "publicstaticccvoidddmain";
        String ragexs = "cc+";
        String[] s = strs.split(ragexs);
        for (String SSSS : s) {
            System.out.println(SSSS);
        }
        System.out.println("=-=========");

    }

    @Test
    public void testStream() {

        String key = "72606_1845";
        String substring = key.substring(key.indexOf("_") + 1);
        System.out.println(substring);


    }


    /**
     * Optional: 可以存放 null
     * orElse:  如果没值，执行的操作
     */
    @Test
    public void testOptional() {
        Optional<String> fullName = Optional.ofNullable(null);
        System.out.println("Full Name is set? " + fullName.isPresent());
        System.out.println("Full Name: " + fullName.orElseGet(() -> "[none]"));
        System.out.println(fullName.map(s -> "Hey " + s + "!").orElse("Hey Stranger!"));
    }

    @Test
    public void testMap() {
        Map map = new HashMap();
        long aa = 1L;

        map.put(aa, "long");

        boolean b = map.containsKey(1);

        System.out.println(b);

    }


    @Test
    public void testCalender() {


        Person p1 = new Person();
        p1.setDate(new Date());
        p1.setId(1);

        Person p2 = new Person();
        p2.setDate(new Date(2000));
        p2.setId(2);

//        Person p3 = new Person();
//        p3.setDate(new Date(3000));
//        p3.setId(3);

        List<Person> listPerson = new ArrayList<>();
        listPerson.add(p1);
//        listPerson.add(p3);
        listPerson.add(p2);

        System.out.println("count ------:" + listPerson.size());

        List<Person> people = listPerson.subList(0, 2);

        System.out.println(people);

        Person p4 = new Person();
        p4.setDate(new Date(3000));
        p4.setId(3);

        listPerson.remove(p4);

//        listPerson = listPerson.stream().filter(Objects::nonNull).filter(c -> c.getId() != p4.getId()).collect(Collectors.toList());
        System.out.println(listPerson);


    }

    @Test
    public void distinctTest() {
        Person p1 = new Person(1, "dY");
        Person p2 = new Person(1, "+0");
        Person p3 = new Person(2, "moon");
        Person p4 = new Person(3, "st");
        List<Person> list = new ArrayList<>();
        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);

        list.stream().forEach(c -> {
            c.setId(10);
        });
        System.out.println(list);

        List<Person> collect = list.stream().distinct().collect(Collectors.toList());
        System.out.println(collect);

    }


    @Test
    public void testList11() {
        List<Double> aaa = new ArrayList<>();
        aaa.add(1.0);
        aaa.add(0.58);
        aaa.add(9.56);
        aaa.add(1.53246);
        aaa.add(1.53247);

        List<Double> collect = aaa.stream().sorted((n1, n2) -> -new Double(n1).compareTo(new Double(n2))).collect(Collectors.toList());

        System.out.println(collect);

        List<Integer> list = Arrays.asList(new Integer[]{1, 9, 4, 6, 2, 7, 5, 3});  //构造list，填充值

        list = list.stream().sorted((n1, n2) -> -n1.compareTo(n2)).collect(Collectors.toList());

        System.out.println(list);

    }


    @Test
    public void testList() {

        for (int i = 0; i < (double) 1 / 100; i++) {
            System.out.println("22222222----------");
        }

        double i = (double) 50 / 100;
        System.out.println(i);

        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);
        localDate = localDate.minusDays(1);
        System.out.println(localDate);


        System.out.println(LocalDate.now().minusDays(-2).format(DateTimeFormatter.ofPattern("yyyyMMdd")));
    }

    /**
     * 流的构造和 map 方法的使用
     * map 和 flatMap 的区别
     * map: 会将一个元素变成一个新的 stream
     * flatMap: 会将结果打平，得到一个单个元素
     */
    @Test
    public void stream1() {

        List<Integer> tetst = new ArrayList<>();
        tetst.add(1);
        tetst.add(2);
        tetst.add(3);

        List<Boolean> collect = tetst.stream().map(c -> c >= 2).collect(Collectors.toList());
        System.out.println(collect);

        List<Integer> collect1 = tetst.stream().filter(c -> c >= 2).collect(Collectors.toList());
        System.out.println(collect);

    }

    /**
     * stream filter 测试
     * 用来 过滤 数据
     */
    @Test
    public void filter() {

        Integer[] sixNums = {1, 2, 3, 4, 5, 6};
        List<Integer> collect = Stream.of(sixNums).filter(m -> m > 3).collect(Collectors.toList());
        System.out.println(collect);

    }

    /**
     * stream foreach 就相当于之前的 超级 for ，遍历
     */
    @Test
    public void foreach() {

        Integer[] sixNums = {1, 2, 3, 4, 5, 6};
        Stream.of(sixNums).filter(n -> n > 2).forEach(p -> System.out.println("this is " + p));

    }

    /**
     * peek：对每一个元素进行操作，并返回一个新的 stream
     */
    @Test
    public void peek() {

        Stream.of("one", "two", "three", "four")
                .filter(e -> e.length() > 3)
                .peek(e -> System.out.println("Filtered value: " + e))
                .map(String::toUpperCase)
                .peek(e -> System.out.println("Mapped value: " + e))
                .collect(Collectors.toList());

    }

    /**
     * 返回 stream 第一个元素
     */
    @Test
    public void findFirst() {

        Optional<String> first = Stream.of("one", "two", "three", "four").findFirst();
        System.out.println(first.get());
    }

    @Test
    public void reduce() {

        // 字符串拼接
        String reduce = Stream.of("A", "B", "C", "D").reduce("", String::concat);
        System.out.println(reduce);

        // 求最小值

    }


}
